package com.zby.lease.web.admin.service.imp;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zby.lease.model.enums.ItemType;
import com.zby.lease.model.pojo.*;
import com.zby.lease.web.admin.mapper.*;
import com.zby.lease.web.admin.service.*;
import com.zby.lease.web.admin.vo.ProvinceVO;
import com.zby.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.zby.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.zby.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.zby.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.zby.lease.web.admin.vo.fee.FeeValueVo;
import com.zby.lease.web.admin.vo.graph.GraphVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
//

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private  GraphInfoMapper graphInfoMapper;
    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;
    @Autowired
    private ApartmentFacilityMapper apiFacilityMapper;
    @Autowired
    private  ApartmentFeeValueMapper apartmentFeeValueMapper;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        boolean isUpdate = apartmentSubmitVo.getId()!=null;
        super.saveOrUpdate(apartmentSubmitVo);
        if (isUpdate){
//            删除图片
            LambdaQueryWrapper<GraphInfo> graphInfoWrapper = new LambdaQueryWrapper<>();
            graphInfoWrapper.eq(GraphInfo::getType, ItemType.APARTMENT);
            graphInfoWrapper.eq(GraphInfo::getItemId, apartmentSubmitVo.getId());
            graphInfoService.remove(graphInfoWrapper);
            //2.删除配套列表
            LambdaQueryWrapper<ApartmentFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
            facilityQueryWrapper.eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId());
            apartmentFacilityService.remove(facilityQueryWrapper);

            //3.删除标签列表
            LambdaQueryWrapper<ApartmentLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
            labelQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId());
            apartmentLabelService.remove(labelQueryWrapper);

            //4.删除杂费列表
            LambdaQueryWrapper<ApartmentFeeValue> feeQueryWrapper = new LambdaQueryWrapper<>();
            feeQueryWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId());
            apartmentFeeValueService.remove(feeQueryWrapper);
        }
        // 获取公寓提交信息中的图表视图列表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        // 检查图表视图列表是否不为空
        if(!CollectionUtils.isEmpty(graphVoList)){
            // 初始化图表信息列表
            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
            // 遍历图表视图列表
            for (GraphVo graphVo : graphVoList){
                // 创建新的图表信息对象
                GraphInfo graphInfo = new GraphInfo();
                // 设置图表信息的URL
                graphInfo.setUrl(graphVo.getUrl());
                // 设置图表信息的名称
                graphInfo.setName(graphVo.getName());
                // 设置图表信息的项目ID为公寓提交信息的ID
                graphInfo.setItemId(apartmentSubmitVo.getId());
                // 设置图表信息的类型为公寓
                graphInfo.setType(ItemType.APARTMENT);
                // 将图表信息添加到列表中
                graphInfos.add(graphInfo);
            }
            // 调用服务层方法，批量保存图表信息
            graphInfoService.saveBatch(graphInfos);
        }
        //2.插入配套列表
//        List<Long> facilityInfoIdList = apartmentSubmitVo.getFacilityInfoIds();
//        if (!CollectionUtils.isEmpty(facilityInfoIdList)){
//            ArrayList<ApartmentFacility> facilityList = new ArrayList<>();
//            for (Long facilityId : facilityInfoIdList) {
//                ApartmentFacility apartmentFacility = new ApartmentFacility();
//                apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
//                apartmentFacility.setFacilityId(facilityId);
//                facilityList.add(apartmentFacility);
//            }
//            apartmentFacilityService.saveBatch(facilityList);
//        }
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        System.out.println("facilityInfoIds");
        System.out.println(facilityInfoIds);
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = new ApartmentFacility();
                apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                apartmentFacility.setFacilityId(facilityInfoId);
                apartmentFacilities.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilities);
        }



        //3.插入标签列表
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)) {
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = new ApartmentLabel();
                apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                apartmentLabel.setLabelId(labelId);
                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }


        //4.插入杂费列表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            ArrayList<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
                apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
                apartmentFeeValue.setFeeValueId(feeValueId);
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }
    }

    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApartmentItemByQuery(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        //查询公寓详情
        ApartmentInfo apartmentInfo = this.getById(id);
        if (apartmentInfo == null){
            return null;
        }
        //2.查询GraphInfo
        List<GraphVo> graphInfoList =   graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT,id);
        //3.查询LabelInfo
        List<LabelInfo> labelInfoList = apartmentLabelMapper.selectListByApartmentId(id);
        //4.查询FacilityInfo
        List<FacilityInfo> facilityInfoList = apiFacilityMapper.selectListByApartmentId(id);
        //5.查询FeeValue
        List<FeeValueVo> feeValueVoList = apartmentFeeValueMapper.selectListByApartmentId(id);

        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;
    }

    @Override
    public void removeApartmentById(Long id) {
        super.removeById(id);
        //1.删除GraphInfo
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getType, ItemType.APARTMENT);
        graphQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphQueryWrapper);

        //2.删除ApartmentLabel
        LambdaQueryWrapper<ApartmentLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
        labelQueryWrapper.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(labelQueryWrapper);

        //3.删除ApartmentFacility
        LambdaQueryWrapper<ApartmentFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
        facilityQueryWrapper.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(facilityQueryWrapper);

        //4.删除ApartmentFeeValue
        LambdaQueryWrapper<ApartmentFeeValue> feeQueryWrapper = new LambdaQueryWrapper<>();
        feeQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(feeQueryWrapper);
        //5.删除ApartmentInfo
        super.removeById(id);
    }

    @Override
    public List<ProvinceInfo> selectAllProvince() {
        return apartmentInfoMapper.selectAllProvince();
    }

    @Override
    public List<ProvinceVO> selectProvinceCityCounty() {
        return apartmentInfoMapper.selectProvinceCityCounty();
    }

    @Override
    public List<CityInfo> selectAllCity() {
        return apartmentInfoMapper.selectAllCity();
    }

    @Override
    public List<DistrictInfo> selectAllDistrict() {
        return apartmentInfoMapper.selectAllDistrict();
    }


}




